<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>企业管理</title>
	<link th:include="common/header::copy"/>

<script type="text/javascript">
        var url;
        $.formatString = function (str) {
            for (var i = 0; i < arguments.length - 1; i++) {
                str = str.replace("{" + i + "}", arguments[i + 1]);
            }
            return str;
        };
        function formatEdit(val,row,index){
            var str = '';
            if ($("#enterpriseupdate").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-edit" onclick="editEnterpriseFun(\'{0}\');" >编辑</a>', index);
            }
            if ($("#enterprisedelete").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-del"  onclick="deleteEnterpriseFun(\'{0}\');" >删除</a>', row.eid);
            }
            return str;
        }
	function searchEnterprise(){
		$("#dg").datagrid('load',{
			"name":$("#s_EnterpriseName").val()
		});
	}
	
	function resetValue(){
		$("#ename").val("");
		$("#ecode").val("");
		$("#legal").val("");
		$("#pic").removeAttr("src");
		$("#etype").val("");
	}
	
	function closeEnterpriseDialog(){
		$("#dlg").dialog("close");
		resetValue();
	}
	
	function saveEnterprise(){
        var file = $("#file")[0].files[0];
		$("#fm").form("submit",{
			url:"/sys/enterprise/add",
            mehtod:"post",
			onSubmit:function(){
				return $(this).form("validate");
			},
			success:function(result){
				var result=eval('('+result+')');
				if(result.success){
					$.messager.alert("系统提示","保存成功！");
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                    resetValue();
				}else{
					$.messager.alert("系统提示",result.info);
				}
			}
		});
	}
	
	function openEnterpriseAddDialog(){
		$("#dlg").dialog("open").dialog("setTitle","添加企业信息");
	}
	
	function editEnterpriseFun(index){
		$("#dg").datagrid(index);
		var row=selectedRows[0];
		$("#pic").attr("src","/image/show?fileName="+row.elicense);
		$("#dlg").dialog("open").dialog("setTitle","修改企业信息");
		$("#fm").form("load",row);
		url="/sys/enterprise/add";
	}
	
	function deleteEnterpriseFun(rid){

		$.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
			if(r){
				$.post("/sys/enterprise/delete",{eid:rid},function(result){
					if(result.success){
						show("删除成功!");
						$("#dg").datagrid("reload");
					}else{
						show(result.info);
					}
				},"json");
			}
		});
	}
</script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="sys:enterprise:delete">
    <input type="hidden" id="enterprisedelete"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:enterprise:update">
    <input type="hidden" id="enterpriseupdate"/>
</shiro:hasPermission>
<table id="dg" title="角色管理" class="easyui-datagrid"
	   data-options="fit:true,fitColumns:true,pagination:true,rownumbers:true,
	   singleSelect:true,url:'/sys/enterprise/list',method:'get',toolbar:'#tb'">
		<thead>
		<tr>
			<th field="eid" width="20" align="center" hidden>编号</th>
			<th field="ename" width="50" align="center">企业名称</th>
			<th field="ctext1" width="50" align="center">企业编码</th>
			<th field="elegal" width="50" align="center">企业法人</th>
			<th field="ecode" width="50" align="center">社会信用代码</th>
			<th field="etype" width="50" align="center">企业类型</th>
			<th field="op" width="50" align="center" formatter="formatEdit">操作</th>
		</tr>
		</thead>
</table>
	
	<div id="tb">
		<div shiro:hasPermission="sys:enterprise:add" style="display: inline-block" >
			<a href="javascript:openEnterpriseAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
		</div>
		<div shiro:hasPermission="sys:enterprise:list" style="display: inline-block;float: right" >
			&nbsp;企业名称：&nbsp;<input type="text" id="s_EnterpriseName" size="20" onkeydown="if(event.keyCode==13) searchEnterprise()"/>
			<a href="javascript:searchEnterprise()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            <a href="javascript:" class="easyui-linkbutton" data-options="plain:true"
               onclick="$('#dg').datagrid('toExcel','enterprise.xls')">下载</a>
		</div>
	</div>
	
	
	<div id="dlg" class="easyui-dialog" style="width: 450px;height: 250px;padding: 10px 20px"
	closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
		<form id="fm" method="post">
            <input type="hidden" id="eid" name="eid">
			<table cellspacing="8px">
				<tr>
					<td>企业名称：</td>
					<td>
						<input type="text" id="ename" name="ename" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
				<tr>
					<td>社会信用代码：</td>
					<td>
						<input type="text" id="ecode" name="ecode" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
                <tr>
                    <td>法人代表：</td>
                    <td>
                        <input type="text" id="elegal" name="elegal" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td><a href="javascript:uploadidalogonline(1)">上传营业执照图片</a></td>
                    <td>
                        <img id="pic" name="pic" style="width: 100px;height: 100px">
                        <input id="elicense" name="elicense" hidden>
                    </td>
                </tr>
                <tr>
                    <td>企业类型：</td>
                    <td>
                        <input type="text" id="etype" name="etype" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
			</table>
		</form>
	</div>
	
	<div id="dlg-buttons">
		<a href="javascript:saveEnterprise()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
		<a href="javascript:closeEnterpriseDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
	</div>
<div id="uploadonlineinfo" class="easyui-dialog" style="width:380px;padding:10px 40px" closed="true">
    <form id="fam" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="file">
        <input type="hidden" id="flag" name="flag">
    </form>

    <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)"
           onclick="uploadonline();" style="width:80px">上传</a>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.enterprise-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'icon-edit'});
                $('.enterprise-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'icon-remove'});
				$('#dg').datagrid('fixRowHeight');
            }
        });
    })
    function uploadidalogonline(flag) {
        $('#uploadonlineinfo').dialog('open').dialog('setTitle', '文件上传');
        $("#flag").val(flag);

    }

    function uploadonline() {
        var file = $("#file")[0].files[0];
        var flag = $("#flag").val();
        var url;
        if (file.name == "") {
            alert("请先选择文件");
            return false;
        }
        if (flag == 1) {
            url = "/image/upload";
            if (!file.name.match(/.jpg|.jpeg|.gif|.png|.bmp/i)) {

                alert("文件格式错误");
                return false;
            }
        } else {
            url = "/sys/enterprise/excelImport";
            if (!file.name.match(/.xls|.xlsx/i)) {
                alert("文件格式错误");
                return false;
            }
        }
        var form = new FormData();
        form.append("file", file);
        var picget = "/image/show?fileName=";
        $.ajax({
            url: url,
            type: "post",
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                $('#uploadonlineinfo').window('close');
                if (flag == 1) {
                    $("#pic").attr("src", picget+data.info);
                    $("#elicense").val(data.info);
                }
                console.log(data);
            },
            error: function (e) {
                console.log(e);
            }
        });

    }
</script>
</body>
</html>